var express = require('express');
var router = express.Router();
var sql = require('../db/index')



/**
 * @swagger
 *   /lunbo/saveOrUpdate:
 *   post:
 *     description: 新增或编辑轮播图
 *     tags: [轮播]
 *     produces:
 *       - application/x-www-form-urlencoded
 *     parameters:
 *       - name: id
 *         description: 轮播图id（编辑时需要）
 *         required: false
 *         type: number
 *       - name: name
 *         description: 轮播图名称
 *         required: true
 *         type: string
 *       - name: description
 *         description: 轮播图描述
 *         required: true
 *         type: string
 *       - name: image
 *         description: 轮播图图片URL
 *         required: true
 *         type: string
 *       - name: status
 *         description: 轮播图状态（正常或停用）
 *         required: true
 *         type: string
 *     responses:
 *       200:
 *         description: 操作成功
 *       500:
 *         description: 服务器错误
 */

// 新增或编辑接口
router.post('/saveOrUpdate', async (req, res) => {
  const { id, name, description, image, status } = req.body;
  let mysql = '';
  if (id) {
    mysql = `UPDATE jz_lunbo SET name = '${name}', description = '${description}', image = '${image}', status = '${status}' WHERE id = ${id}`;
  } else {
    mysql = `INSERT INTO jz_lunbo (id, name, description, image, status) VALUES (null, '${name}', '${description}', '${image}', '${status}')`;
  }
  await sql.query(mysql);
  res.send({
    status: 200,
    message: "操作成功",
    data: null,
    timestamp: new Date().getTime()
  });
});

/**
 * @swagger
 *   /lunbo/delete:
 *   post:
 *     description: 删除轮播图
 *     tags: [轮播]
 *     produces:
 *       - application/x-www-form-urlencoded
 *     parameters:
 *       - name: id
 *         description: 轮播图id
 *         required: true
 *         type: number
 *     responses:
 *       200:
 *         description: 删除成功
 *       500:
 *         description: 服务器错误
 */

// 删除接口
router.post('/delete', async (req, res) => {
  const { id } = req.body;
  const mysql = `DELETE FROM jz_lunbo WHERE id = ${id}`;
  await sql.query(mysql);
  res.send({
    status: 200,
    message: '删除成功',
    data: null,
    timestamp: new Date().getTime()
  });
});

/**
 * @swagger
 *   /lunbo/disable:
 *   post:
 *     description: 停用轮播图
 *     tags: [轮播]
 *     produces:
 *       - application/x-www-form-urlencoded
 *     parameters:
 *       - name: id
 *         description: 轮播图id
 *         required: true
 *         type: number
 *     responses:
 *       200:
 *         description: 停用成功
 *       500:
 *         description: 服务器错误
 */

// 停用接口
router.post('/disable', async (req, res) => {
  const { id } = req.body;
  const mysql = `UPDATE jz_lunbo SET status = '停用' WHERE id = ${id}`;
  await sql.query(mysql);
  res.send({
    status: 200,
    message: '停用成功',
    data: null,
    timestamp: new Date().getTime()
  });
});

module.exports = router;








